package org.example.ExperimentFifth;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

public class TestTime {
    public static void main(String[] args) {
//        创建ArrayList、LinkedList
        ArrayList<Integer> arrayList = new ArrayList<>();
        LinkedList<Integer> linkedList = new LinkedList<>();
//        创建随机数
        Random random = new Random();

        // arrayList中插入100000个随机整数
        for (int i = 0; i < 100000; i++) {
            int randomNumber = random.nextInt();
            arrayList.add(randomNumber);
        }

        // linkedList中插入100000个随机整数
        for (int i = 0; i < 100000; i++) {
            int randomNumber = random.nextInt();
            linkedList.add(randomNumber);
        }
        //arrayList中插入1000个新元素所需的时间
        long startTime1 = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            int randomNumber = random.nextInt();
            arrayList.add(randomNumber);
        }
        long endTime1 = System.nanoTime();
        long durationArrayListAdd = endTime1 - startTime1;
        System.out.println("arrayList中插入1000个新元素所需的时间：" + durationArrayListAdd + "纳秒");

        // linkedList中插入1000个新元素所需的时间
        long startTime2 = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            int randomNumber = random.nextInt();
            linkedList.add(randomNumber);
        }
        long endTime2 = System.nanoTime();
        long durationLinkedListAdd = endTime2 - startTime2;
        System.out.println("linkedList中插入1000个新元素所需的时间：" + durationLinkedListAdd + "纳秒");

        // arrayList中删除1000个元素所需的时间
        long startTime3 = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            arrayList.remove(arrayList.size() - 1);
        }
        long endTime3 = System.nanoTime();
        long durationArrayListRemove = endTime3 - startTime3;
        System.out.println("arrayList中删除1000个元素所需的时间：" + durationArrayListRemove + "纳秒");

        // linkedList中删除1000个元素所需的时间
        long startTime4 = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            linkedList.remove(linkedList.size() - 1);
        }
        long endTime4 = System.nanoTime();
        long durationLinkedListRemove = endTime4 - startTime4;
        System.out.println("linkedList中删除1000个元素所需的时间：" + durationLinkedListRemove + "纳秒");
    }
}
